<?php
/**
 * Author: Hancock
 * Email: 84622365@qq.com
 * Date: 3/23/2016
 * Des: 测试控制器
 */
defined('INHkMvc') or exit('Access Invalid!');

class ApiController extends Controller
{
    private $path = UPLOAD_ORDER_PATH;
    private $ad_zone_id = '65684557';

    //短信
    public function qrcodeAct()
    {
        header('Content-Type:text/html; charset=utf-8');
        $auth = trim($this->_getParam('auth', ''));
        $app = trim($this->_getParam("app", ""));
        if ($auth == "" || $auth != 'qrcode720' || $app == "") {
            echo "invalid";
            exit();
        }
        $mobile = file_get_contents(ROOT_PATH . 'data' . SEPARATE . 'mobile' . SEPARATE . $app . ".ini");
        $res = smsSend(rtrim($mobile, ','), ['app' => $app], 'qrcode');
        if (array_key_exists("status", $res) && $res['status'] == 1) {
            echo "true";
            exit();
        } else {
            echo "false";
            exit();
        }
    }

    public function showAct()
    {
        header('Content-Type:text/html; charset=utf-8');
        $app = $this->_getParam("app", "");
        echo '<div style="margin:0 auto 15px;text-align:center;">' . $app . '二维码' . ((file_get_contents($app . "scan.ini") == 1) ? "" : "过期") . '</div>';
        echo '<div style="margin:auto;text-align:center;"><img src="data/upload/qrcode/' . $app . '/qrcode.jpg"></div>';
        exit();
    }

    public function scanAct()
    {
        if (!(isset($_GET['auth']) && $_GET['auth'] == 'qrcode720')) {
            echo "invalid";
            exit();
        }
        $app = isset($_GET['app']) ? trim($_GET['app']) : 0;
        if ($app === 0) {
            echo "false";
            exit();
        }
        $scan = isset($_GET['do']) ? intval($_GET['do']) : 0;
        if (file_put_contents($app . "scan.ini", $scan)) {
            echo "true";
            exit();
        } else {
            echo "false";
            exit();
        }
    }

    //更新计划
    public function updatePlanAct()
    {
        include_once LIBS_PATH . 'class' . SEPARATE . 'log.class.php';
        $log = new log();

        $mod = M('app.good_plan');
        $res = $mod->update(['task_status' => 0]);
        if ($res === false) {
            exit('reset failed' . PHP_EOL);
        }

        $data = taobaoCurl('plan', ['page' => 1, 'page_size' => 40]);

        if (json_last_error() != 0) {
            $log->write("page 1 deny", 2, "update_plan.log");
            exit('deny' . PHP_EOL);
        }

        if (!isset($data['data']['pagelist']) && !$data['data']['pagelist']) {
            $log->write("no data", 2, "update_plan.log");
            exit('no data' . PHP_EOL);
        }

        $mod = M('app.good_plan');

        $hasNextPage = $data['data']['hasNextPage'];

        $i = 0;
        while ($hasNextPage) {
            if ($i > 0) {
                sleep(6);
                $data = taobaoCurl('plan', ['page' => $i + 1, 'page_size' => 40]);
                if (json_last_error() != 0) {
                    $log->write("page " . ($i + 1) . " deny", 2, "update_plan.log");
                    exit('deny');
                }
                if (!isset($data['data']['pagelist']) && !$data['data']['pagelist']) {
                    $log->write("page " . ($i + 1) . " no data", 2, "update_plan.log");
                    exit('no data');
                }
            }

            $good = $data['data']['pagelist'];
            $mod->begin();
            foreach ($good as $k => $v) {
                $good_plan = $v;
                $good_plan['plan_id'] = $v['id'];
                $good_plan['createTime'] = strtotime($v['createTime']);
                $good_plan['updateTime'] = strtotime($v['updateTime']);
                $good_plan['sbegintime'] = strtotime($v['sbegintime']);
                $good_plan['sendtime'] = strtotime($v['sendtime']);
                $good_plan['task_status'] = 2;
                $plan_id = $mod->find(['plan_id' => $v['id']])['id'];
                unset($good_plan['id']);
                if (intval($plan_id) == 0) {
                    $res = $mod->insert($good_plan);
                } else {
                    $res = $mod->update($good_plan, ['id' => $plan_id]);
                }
                if ($res === false) {
                    $log->write("plan:" . $v['id'] . " failed", 2, "update_plan.log");
                }
            }
            $hasNextPage = $data['data']['hasNextPage'];
            $i++;
            $mod->commit();
        }
        //接口变了
        /**
         * for ($i = 0; $i < $count; $i++) {
         * if ($i > 0) {
         * sleep(6);
         * $data = taobaoCurl('plan', ['page' => $i + 1, 'page_size' => 40]);
         * if (json_last_error() != 0) {
         * $log->write("page " . ($i + 1) . " deny", 2, "update_plan.log");
         * exit('deny');
         * }
         * if (!isset($data['data']['pagelist']) && !$data['data']['pagelist']) {
         * $log->write("page " . ($i + 1) . " no data", 2, "update_plan.log");
         * exit('no data');
         * }
         * }
         *
         * $good = $data['data']['pagelist'];
         * $mod->begin();
         * foreach ($good as $k => $v) {
         * $good_plan = $v;
         * $good_plan['plan_id'] = $v['id'];
         * $good_plan['createTime'] = strtotime($v['createTime']);
         * $good_plan['updateTime'] = strtotime($v['updateTime']);
         * $good_plan['sbegintime'] = strtotime($v['sbegintime']);
         * $good_plan['sendtime'] = strtotime($v['sendtime']);
         * $good_plan['task_status'] = 2;
         * $plan_id = $mod->find(['plan_id' => $v['id']])['id'];
         * unset($good_plan['id']);
         * if (intval($plan_id) == 0) {
         * $res = $mod->insert($good_plan);
         * } else {
         * $res = $mod->update($good_plan, ['id' => $plan_id]);
         * }
         * if ($res === false) {
         * $log->write("plan:" . $v['id'] . " failed", 2, "update_plan.log");
         * }
         * }
         * $mod->commit();
         * }
         **/
        exit("success" . PHP_EOL);
    }

    //更新产品
    public function updateGoodAct()
    {
        include_once LIBS_PATH . 'class' . SEPARATE . 'log.class.php';
        $log = new log();
        $mod = M('app.good_plan');
        $res = $mod->order('sort desc,id desc')->select(['task_status' => 2]);
        if ($res === false) {
            $log->write("get plan failed", 2, "update_good.log");
            exit('get plan failed' . PHP_EOL);
        }

        $flag_time = time();

        foreach ($res as $k => $p) {
            if ($flag_time - time() > 1140) {
                sleep(120);
                $flag_time = time();
            }
            $plan_done = 1;
            echo $p['plan_id'] . " start>>>" . " test" . PHP_EOL;

            $data = taobaoCurl('good', ['campaignId' => $p['campaignId'], 'shopKeeperId' => $p['shopKeeperId'], 'userNumberId' => $p['oriMemberId'], 'page' => 1]);

            if (json_last_error() != 0) {
                $log->write("campaignId: " . $p['campaignId'] . " page 1 deny", 2, "update_good.log");
                $plan_done = 0;
            }
            $is_update = M('app.good')->updateGoodByComcampaignId($p['campaignId']);
            if ($is_update == false) {
                $log->write("updateGood_is_update: " . $p['campaignId'] . "failed", 2, "update_good.log");
            }
            $count = $data['data']['paginator']['lastPage'];
            for ($i = 0; $i < $count; $i++) {
                if ($i % 20 == 0 && $i != 0) {
                    sleep(80);
                }

                if ($i != 0) {
                    $data = taobaoCurl('good', ['campaignId' => $p['campaignId'], 'shopKeeperId' => $p['shopKeeperId'], 'userNumberId' => $p['oriMemberId'], 'page' => $i + 1]);
                    if (json_last_error() != 0) {
                        $log->write("campaignId: " . $p['campaignId'] . " page " . ($i + 1) . " deny", 2, "update_good.log");
                        $plan_done = 0;
                    }
                }

                if ($data == "" || !array_key_exists("pagelist", $data['data'])) {
                    $log->write("campaignId: " . $p['campaignId'] . " page " . ($i + 1) . " no data", 2, "update_good.log");
                    echo "campaignId: " . $p['campaignId'] . " page " . ($i + 1) . " no data" . PHP_EOL;
                    $plan_done = 0;
                    continue;
                }
                $good = $data['data']['pagelist'];
                $mod = M('app.good');

                if ($i != 0) {
                    sleep(80);
                }

                foreach ($good as $kk => $v) {

                    echo $v['auctionId'] . " test" . PHP_EOL;

                    $tbk_info = [];
                    $tbk_info['seller_id'] = $data['data']['oriMemberid'];
                    $tbk_info['nick'] = $v['nick'];
                    $tbk_info['user_type'] = $v['userType'];
                    $tbk_info['zk_final_price'] = $v['zkPrice'];
                    $tbk_info['num_iid'] = $v['auctionId'];
                    $tbk_info['title'] = $v['title'];
                    $tbk_info['pict_url'] = (strstr($v['pictUrl'], "http:")) ? $v['pictUrl'] : "http:" . $v['pictUrl'];
                    $tbk_info['item_url'] = $v['auctionUrl'];
                    $tbk_info['reserve_price'] = $v['reservePrice'];
                    $tbk_info['define_tk_rate'] = $v['commissionRatePercent'];
                    $tbk_info['add_time'] = time();
                    $tbk_info['is_update'] = 1;
                    $tbk_info['good_type'] = 1;
                    $tbk_info['dxjh_id'] = $p['campaignId'];
                    if ($v['commissionRatePercent'] >= 10 && $v['commissionRatePercent'] < 15) {
                        $rate = M('app.rate')->find(['tk_rate' => '10,15'])['fee'];
                        $tbk_info['tk_rate'] = $v['commissionRatePercent'] * $rate;
                    } else if ($v['commissionRatePercent'] >= 15 && $v['commissionRatePercent'] < 20) {
                        $rate = M('app.rate')->find(['tk_rate' => '15,20'])['fee'];
                        $tbk_info['tk_rate'] = $v['commissionRatePercent'] * $rate;
                    } else if ($v['commissionRatePercent'] >= 20 && $v['commissionRatePercent'] < 30) {
                        $rate = M('app.rate')->find(['tk_rate' => '20,30'])['fee'];
                        $tbk_info['tk_rate'] = $v['commissionRatePercent'] * $rate;
                    } else if ($v['commissionRatePercent'] >= 30) {
                        $rate = M('app.rate')->find(['tk_rate' => '30'])['fee'];
                        $tbk_info['tk_rate'] = $v['commissionRatePercent'] * $rate;
                    } else {
                        $tbk_info['tk_rate'] = $v['commissionRatePercent'];
                    }
                    $res = $mod->addPlanGood($tbk_info);
                    if ($res === false) {
                        $log->write("update num_iid:" . $v['auctionId'] . " failed", 2, "update_good.log");
                        echo("update num_iid:" . $v['auctionId'] . ' failed' . PHP_EOL);
                    } else {
                        $res_check = M('app.good_2_vip')->checkUrl($res);
                        if ($res_check == "new" || $res_check == "empty") {
                            $url_info = taobaoCurl('click', ['num_iid' => $tbk_info['num_iid'], 'ad_zone_id' => $this->ad_zone_id, 'site_id' => '18564365']);
                            if (is_array($url_info) && array_key_exists('ok', $url_info) && $url_info['ok'] == 1) {
                                $res_up_url = M('app.good_2_vip')->updateDefaultUrl($url_info['data'], $res, $res_check);
                                if ($res_up_url === false) {
                                    $log->write("update num_iid:" . $v['auctionId'] . " url failed", 2, "update_good.log");
                                    echo("update num_iid:" . $v['auctionId'] . ' url failed' . PHP_EOL);
                                }
                            } else {
                                continue;
                            }
                        }
                    }
                }
            }
            if ($plan_done) {
                M("app.good_plan")->update(['task_status' => 1], ['id' => $p['id']]);
            }
            $status = M('app.good')->updateStatusByComcampaignId($p['campaignId']);
            if ($status == false) {
                $log->write("updateGood_status: " . $p['campaignId'] . "failed", 2, "update_good.log");
            }
        }
        exit("success" . PHP_EOL);
    }

    //获取用户产品推广链接
    public function getClickAct()
    {
        $app = file_get_contents("C:/py/deka/host.ini");
        //登录锁
        if (file_get_contents('http://crm.720baba.com/data/upload' . SEPARATE . 'qrcode' . SEPARATE . $app . SEPARATE . 'tb.lock')) {
            ajaxReturn(201, "login invalid");
        }
        $num_iid = trim($_POST['num_iid']);
        $ad_zone_id = trim($_POST['ad_zone_id']);
        $site_id = (isset($_POST['site_id']) && strlen(trim($_POST['site_id']))) ? trim($_POST['site_id']) : '18564365';
        ajaxReturn(200, taobaoCurl('click', ['num_iid' => $num_iid, 'ad_zone_id' => $ad_zone_id, 'site_id' => $site_id]));
    }

    //获取用户产品推广链接
    public function getGoodsUrlAct()
    {
        $num_iid = trim($_POST['num_iid']);
        $perPageSize = trim($_POST['perPageSize']);
        $shopTag = trim($_POST['shopTag']);
        ajaxReturn(200, taobaoCurl('goods', ['num_iid' => $num_iid, 'perPageSize' => $perPageSize, 'shopTag' => $shopTag]));
    }

    //获取用户产品推广链接
    public function getUpdateGoodUrlAct()
    {
        $num_iid = trim($_POST['num_iid']);
        $perPageSize = trim($_POST['perPageSize']);
        $shopTag = trim($_POST['shopTag']);
        ajaxReturn(200, taobaoCurl('goodUpdate', ['num_iid' => $num_iid, 'perPageSize' => $perPageSize, 'shopTag' => $shopTag]));
    }

    //获取用户订单
    public function getMemberAllOrderAct()
    {
        $days = 5;
        $end_date = date("Y-m-d", time());
        $start_date = $end_date;
        $data = [];
        for ($i = 0; $i < $days; $i++) {
            $current = date("Y-m-d", strtotime($end_date) - 86400 * $i);
            $start_date = $current;
            $data[] = taobaoCurl('order', ['start_date' => $current, 'end_date' => $current]);
        }
        ajaxReturn(200, $data);
    }

    public function updateOrderAct()
    {

        set_time_limit(0);
        include_once LIBS_PATH . 'class' . SEPARATE . 'log.class.php';
        $log = new log();

        $app = file_get_contents("C:/py/deka/host.ini");
        if ($app == false) {
            $log->write("app is not found", 2, 'update_order.log');
            echo "app is not found" . PHP_EOL;
            exit();
        }

        //登录锁
        if (file_get_contents('http://crm.720baba.com/data/upload' . SEPARATE . 'qrcode' . SEPARATE . $app . SEPARATE . 'tb.lock')) {
            $log->write("tb token is pass time", 2, 'update_order.log');
            echo "tb token is pass time" . PHP_EOL;
            exit();
        }

        $active = trim(file_get_contents("http://newtc.720baba.com/?con=api&act=getAppActive"));
        if ($active !== '1') {
            echo "service host changed";
            $log->write("service host changed", 2, 'update_order.log');
            exit();
        }

        $root = $this->path;
        $days = 1;
        $end_date = date("Y-m-d", time());
        $start_date = $end_date;
        for ($i = 0; $i < $days; $i++) {
            $current = date("Y-m-d", strtotime($end_date) - 86400 * $i);
            $start_date = $current;
            $file_path = $root . str_replace('-', '/', $current);
            if (!is_dir($file_path)) {
                makeDir($file_path, 0777, true);
            }
            $data = taobaoCurl('order', ['start_date' => $current, 'end_date' => $current]);
            file_put_contents($file_path . '/order.xls', $data);
        }

//        exec("chmod -R 0777 " . UPLOAD_ORDER_PATH);
//        $opts = array(
//            'http' => array(
//                'method' => "GET",
//                'timeout' => 900,
//            )
//        );
//        $context = stream_context_create($opts);
//        $res = json_decode(file_get_contents($update_url . "&start_date=" . $start_date . "&end_date=" . $end_date, false, $context), 1);

        $cmd = "C:/wamp64/bin/php/php7.0.10/php.exe -f C:/wamp64/www/vr_crm/index.php api uploadMemberOrder" . SPACE . $start_date . SPACE . $end_date;

        exec($cmd, $output, $status);

        if (!(is_array($output) && $output[0] == "success")) {
            $log->write("update $start_date-$end_date order failed. " . serialize($output), 2, 'update_order.log');
        }
        echo $output[0];
        exit();
    }

    public function uploadMemberOrderAct()
    {

        if (!isset($_SESSION['argv'][4])) {
            echo "param" . PHP_EOL;
            exit();
        }

        set_time_limit(0);
        $app = file_get_contents("C:/py/deka/host.ini");
        if ($app == false) {
            echo "app" . PHP_EOL;
            exit();
        }
        $order_lock_url = 'http://crm.720baba.com/data/upload' . SEPARATE . 'qrcode' . SEPARATE . $app . SEPARATE . 'order.lock';
        $lock = (int)trim(file_get_contents($order_lock_url));

        while ($lock == 1) {
            echo "process is locked" . PHP_EOL;
            sleep(500);
            $lock = (int)trim(file_get_contents($order_lock_url));
        }

        $root = rtrim($this->path, '/');

        $start_date = $_SESSION['argv'][3];
        $end_date = $_SESSION['argv'][4];

        $files = [];

        $start_date = strtotime($start_date);
        $end_date = strtotime($end_date);

        $file_name = getFilePathByTime($root, $start_date, $end_date);
        for ($i = 0; $i < count($file_name); $i++) {
            $files[] = my_scandir($file_name[$i], $root);
        }
        if (!empty($files)) {
            //加锁
            $lk = file_get_contents("http://crm.720baba.com/?con=task&act=orderLock&app=".$app."&lock=1");
            if ($lk == false) {
                echo "lock";
                exit();
            }
            foreach ($files as $k => $v) {
                if (!empty($v)) {
                    for ($j = 0; $j < count($v); $j++) {
                        $msg = M('app.member_order')->addExcelInfo($v[$j]['file_name'], 0, $v[$j]['file_time'], 0, $app);
                        if (strstr($msg, 'date')) {
                            //解锁
                            $lk = file_get_contents("http://crm.720baba.com/?con=task&act=orderLock&app=".$app);
                            if ($lk == false) {
                                echo "unlock";
                                exit();
                            }
                            echo "date";
                            exit();
                        } elseif ($msg == false) {
                            //解锁
                            $lk = file_get_contents("http://crm.720baba.com/?con=task&act=orderLock&app=".$app);
                            if ($lk == false) {
                                echo "unlock";
                                exit();
                            }
                            echo "failed";
                            exit();
                        }
                    }
                }
            }
            //解锁
            $lk = file_get_contents("http://crm.720baba.com/?con=task&act=orderLock&app=".$app);
            if ($lk == false) {
                echo "unlock";
                exit();
            }
        }
        echo "success";
        exit();
    }

    public function updatePlanByIdAct()
    {

        include_once LIBS_PATH . 'class' . SEPARATE . 'log.class.php';
        $log = new log();

        $id = $_SESSION['argv'][3];

        $mod = M("app.good_plan");
        $plan = $mod->find(['id' => $id]);

        if (!M("app.good")->update(['is_update' => 0, 'is_delete' => 1], ['dxjh_id' => $plan['campaignId']])) {
            $log->write("failed plan:" . $id . " reset", 2, "update_extra_plan.log");
            echo "reset failed plan:" . $id . PHP_EOL;
        }

        if ($plan == false) {
            $mod->update(['task_status' => 3], ['id' => $id]);
            $log->write("failed plan:" . $id, 2, "update_extra_plan.log");
            echo "failed plan:" . $id . PHP_EOL;
        }

        $data = taobaoCurl('good', ['campaignId' => $plan['campaignId'], 'shopKeeperId' => $plan['shopKeeperId'], 'userNumberId' => $plan['oriMemberId'], 'page' => 1]);

        if (json_last_error() != 0) {
            $log->write("plan:" . $id . " page 1 deny", 2, "update_extra_plan.log");
            echo "plan:" . $id . " page 1 deny" . PHP_EOL;
        }

        $count = $data['data']['paginator']['lastPage'];
        $is_update = M('app.good')->updateGoodByComcampaignId($plan['campaignId']);
        if ($is_update == false) {
            $log->write("updatePlanById_is_update: " . $plan['campaignId'] . "failed", 2, "update_good.log");
        }
        for ($i = 0; $i < $count; $i++) {
            if ($i % 20 == 0 && $i != 0) {
                sleep(5);
            }

            if ($i != 0) {
                $data = taobaoCurl('good', ['campaignId' => $plan['campaignId'], 'shopKeeperId' => $plan['shopKeeperId'], 'userNumberId' => $plan['oriMemberId'], 'page' => $i + 1]);
                if (json_last_error() != 0) {
                    $log->write("plan:" . $id . " page " . ($i + 1) . " deny", 2, "update_extra_plan.log");
                    echo "plan:" . $id . " page " . ($i + 1) . " deny" . PHP_EOL;
                }
            }

            $good = $data['data']['pagelist'];
            $mod = M('app.good');
            foreach ($good as $v) {
                $tbk_info = [];
                $tbk_info['seller_id'] = $data['data']['oriMemberid'];
                $tbk_info['nick'] = $v['nick'];
                $tbk_info['user_type'] = $v['userType'];
                $tbk_info['zk_final_price'] = $v['zkPrice'];
                $tbk_info['num_iid'] = $v['auctionId'];
                $tbk_info['title'] = $v['title'];
                $tbk_info['pict_url'] = (strstr($v['pictUrl'], "http:")) ? $v['pictUrl'] : "http:" . $v['pictUrl'];
                $tbk_info['item_url'] = $v['auctionUrl'];
                $tbk_info['reserve_price'] = $v['reservePrice'];
                $tbk_info['define_tk_rate'] = $v['commissionRatePercent'];
                $tbk_info['add_time'] = time();
                $tbk_info['is_update'] = 1;
                $tbk_info['is_delete'] = 0;
                $tbk_info['good_type'] = 1;
                $tbk_info['dxjh_id'] = $plan['campaignId'];
                if ($v['commissionRatePercent'] >= 10 && $v['commissionRatePercent'] < 15) {
                    $rate = M('app.rate')->find(['tk_rate' => '10,15'])['fee'];
                    $tbk_info['tk_rate'] = $v['commissionRatePercent'] * $rate;
                } else if ($v['commissionRatePercent'] >= 15 && $v['commissionRatePercent'] < 20) {
                    $rate = M('app.rate')->find(['tk_rate' => '15,20'])['fee'];
                    $tbk_info['tk_rate'] = $v['commissionRatePercent'] * $rate;
                } else if ($v['commissionRatePercent'] >= 20 && $v['commissionRatePercent'] < 30) {
                    $rate = M('app.rate')->find(['tk_rate' => '20,30'])['fee'];
                    $tbk_info['tk_rate'] = $v['commissionRatePercent'] * $rate;
                } else if ($v['commissionRatePercent'] >= 30) {
                    $rate = M('app.rate')->find(['tk_rate' => '30'])['fee'];
                    $tbk_info['tk_rate'] = $v['commissionRatePercent'] * $rate;
                } else {
                    $tbk_info['tk_rate'] = $v['commissionRatePercent'];
                }
                $res = $mod->addPlanGood($tbk_info);
                if ($res === false) {
                    $log->write("update num_iid:" . $v['auctionId'] . " failed", 2, "update_extra_plan.log");
                    echo "update num_iid:" . $v['auctionId'] . ' failed' . PHP_EOL;
                }
            }
        }
        $status = M('app.good')->updateStatusByComcampaignId($plan['campaignId']);
        if ($status == false) {
            $log->write("updatePlanById_status: " . $plan['campaignId'] . "failed", 2, "update_good.log");
        }
        M("app.good_plan")->update(['task_status' => 1], ['id' => $plan['id']]);
    }

    public function taskAct()
    {
        $data = M("app.good_plan")->field("id")->order('sort desc,id desc')->select(['task_status' => 2]);
        $mod_task = M('app.task_good');
        $mod_task->updateGoodStatus();
        foreach ($data as $key => $value) {
            $task_id = $mod_task->updateGoodPlan($value['id']);
            if ($task_id === false) {
                continue;
            }
        }
    }

    public function extraPlanUpdate()
    {
        $data = M("app.good_plan")->field("id")->order('sort desc,id desc')->select(['task_status' => 2]);
        $mod_task = M('app.task_good');
        $mod_task->updateGoodStatus();
        foreach ($data as $key => $value) {

            include_once LIBS_PATH . 'class' . SEPARATE . 'log.class.php';
            $log = new log();

            $id = $data['id'];

            $mod = M("app.good_plan");
            $plan = $mod->find(['id' => $id]);

            if (!M("app.good")->update(['is_update' => 0, 'is_delete' => 1], ['dxjh_id' => $plan['campaignId']])) {
                $log->write("failed plan:" . $id . " reset", 2, "update_extra_plan.log");
                echo "reset failed plan:" . $id . PHP_EOL;
            }

            if ($plan == false) {
                $mod->update(['task_status' => 3], ['id' => $id]);
                $log->write("failed plan:" . $id, 2, "update_extra_plan.log");
                echo "failed plan:" . $id . PHP_EOL;
            }

            $data = taobaoCurl('good', ['campaignId' => $plan['campaignId'], 'shopKeeperId' => $plan['shopKeeperId'], 'userNumberId' => $plan['oriMemberId'], 'page' => 1]);

            if (json_last_error() != 0) {
                $log->write("plan:" . $id . " page 1 deny", 2, "update_extra_plan.log");
                echo "plan:" . $id . " page 1 deny" . PHP_EOL;
            }

            $count = $data['data']['paginator']['lastPage'];
            $is_update = M('app.good')->updateGoodByComcampaignId($plan['campaignId']);
            if ($is_update == false) {
                $log->write("updatePlanById_is_update: " . $plan['campaignId'] . "failed", 2, "update_good.log");
            }
            for ($i = 0; $i < $count; $i++) {
                if ($i % 20 == 0 && $i != 0) {
                    sleep(5);
                }

                if ($i != 0) {
                    $data = taobaoCurl('good', ['campaignId' => $plan['campaignId'], 'shopKeeperId' => $plan['shopKeeperId'], 'userNumberId' => $plan['oriMemberId'], 'page' => $i + 1]);
                    if (json_last_error() != 0) {
                        $log->write("plan:" . $id . " page " . ($i + 1) . " deny", 2, "update_extra_plan.log");
                        echo "plan:" . $id . " page " . ($i + 1) . " deny" . PHP_EOL;
                    }
                }

                $good = $data['data']['pagelist'];
                $mod = M('app.good');
                foreach ($good as $v) {
                    $tbk_info = [];
                    $tbk_info['seller_id'] = $data['data']['oriMemberid'];
                    $tbk_info['nick'] = $v['nick'];
                    $tbk_info['user_type'] = $v['userType'];
                    $tbk_info['zk_final_price'] = $v['zkPrice'];
                    $tbk_info['num_iid'] = $v['auctionId'];
                    $tbk_info['title'] = $v['title'];
                    $tbk_info['pict_url'] = (strstr($v['pictUrl'], "http:")) ? $v['pictUrl'] : "http:" . $v['pictUrl'];
                    $tbk_info['item_url'] = $v['auctionUrl'];
                    $tbk_info['reserve_price'] = $v['reservePrice'];
                    $tbk_info['define_tk_rate'] = $v['commissionRatePercent'];
                    $tbk_info['add_time'] = time();
                    $tbk_info['is_update'] = 1;
                    $tbk_info['is_delete'] = 0;
                    $tbk_info['good_type'] = 1;
                    $tbk_info['dxjh_id'] = $plan['campaignId'];
                    if ($v['commissionRatePercent'] >= 10 && $v['commissionRatePercent'] < 15) {
                        $rate = M('app.rate')->find(['tk_rate' => '10,15'])['fee'];
                        $tbk_info['tk_rate'] = $v['commissionRatePercent'] * $rate;
                    } else if ($v['commissionRatePercent'] >= 15 && $v['commissionRatePercent'] < 20) {
                        $rate = M('app.rate')->find(['tk_rate' => '15,20'])['fee'];
                        $tbk_info['tk_rate'] = $v['commissionRatePercent'] * $rate;
                    } else if ($v['commissionRatePercent'] >= 20 && $v['commissionRatePercent'] < 30) {
                        $rate = M('app.rate')->find(['tk_rate' => '20,30'])['fee'];
                        $tbk_info['tk_rate'] = $v['commissionRatePercent'] * $rate;
                    } else if ($v['commissionRatePercent'] >= 30) {
                        $rate = M('app.rate')->find(['tk_rate' => '30'])['fee'];
                        $tbk_info['tk_rate'] = $v['commissionRatePercent'] * $rate;
                    } else {
                        $tbk_info['tk_rate'] = $v['commissionRatePercent'];
                    }
                    $res = $mod->addPlanGood($tbk_info);
                    if ($res === false) {
                        $log->write("update num_iid:" . $v['auctionId'] . " failed", 2, "update_extra_plan.log");
                        echo "update num_iid:" . $v['auctionId'] . ' failed' . PHP_EOL;
                    }
                }
            }
            $status = M('app.good')->updateStatusByComcampaignId($plan['campaignId']);
            if ($status == false) {
                $log->write("updatePlanById_status: " . $plan['campaignId'] . "failed", 2, "update_good.log");
            }
            M("app.good_plan")->update(['task_status' => 1], ['id' => $plan['id']]);
            sleep(60);
        }
    }

    public function updateGoodStatusAct()
    {
        include_once LIBS_PATH . 'class' . SEPARATE . 'log.class.php';
        $log = new log();
        $res = M("app.good")->updateDxjhPlanGood();
        if ($res === false) {
            $log->write("update plan good status failed", 2, "update_extra_plan.log");
            echo "failed";
        }
        echo "success";
    }

    public function updateRedAct()
    {
        $ad_zone_ids = M("app.member_vip")->field("member_id,ad_zone_id")->select(["is_red" => 0]);
        $count = sizeof($ad_zone_ids);
        echo $count . PHP_EOL;
        $num = 0;
        foreach ($ad_zone_ids as $v) {
            $data = taobaoCurl("red", ['ad_zone_id' => $v['ad_zone_id']]);
            if (array_key_exists('ok', $data) && $data['ok'] === "true") {
                $data['member_id'] = $v['member_id'];
                $data['ad_zone_id'] = $v['ad_zone_id'];
                $res = M("app.member_red_packet")->editMemberRedPacket($data);
                if ($res) {
                    M("app.member_vip")->update(['is_red' => 1], ['ad_zone_id' => $v['ad_zone_id']]);
                    echo ++$num . "/" . $count . PHP_EOL;
                } else {
                    echo $v['member_id'] . "-" . $v['ad_zone_id'] . " failed;" . PHP_EOL;
                    die;
                }
            } else {
                echo $v['member_id'] . "-" . $v['ad_zone_id'] . " failed;" . PHP_EOL;
                die;
            }
        }
        echo "task over";
        die;
    }

    public function isActiveAct()
    {
        header('Content-Type:text/html; charset=utf-8');
        $active = trim(file_get_contents("http://newtc.720baba.com/?con=api&act=getAppActive"));
        if ($active === '1') {
            echo 1;
        } else {
            echo 0;
        }
        exit();
    }

    public function getAppActiveAct()
    {
        $ip = get_client_ip();
        if (M('app.tb_ip')->isActiveByIp($ip)) {
            echo 1;
        } else {
            echo 0;
        }
        exit();
    }

    public function getAppAct()
    {
        $ip = get_client_ip();
        $app = M("app.tb_ip")->getAppName($ip);
        if ($app) {
            echo $app['app'];
        } else {
            echo 0;
        }
        die;
    }

    public function getIpAct()
    {
        echo get_client_ip();
        exit;
    }
}
